//
//  PETableMoreView.m
//  EWallet
//
//  Created by hemingway payegis on 13-1-7.
//  Copyright (c) 2013年 payegis. All rights reserved.
//

#import "PETableMoreView.h"
#import "UIView+Additional.h"

#define PETableMoreButtonTitleNormal @"更多"
#define PETableMoreButtonTitleLoading @"正在载入"

@interface PETableMoreView ()
{
    UIButton* _moreButton;
    BOOL _isLoading;
}

- (void)moreButtonClicked;
- (void)setTableMoreViewState:(PETableMoreViewState)state;
@end

@implementation PETableMoreView
@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _isLoading = NO;
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _moreButton.frame = self.bounds;
        _moreButton.backgroundColor = [UIColor clearColor];
        [_moreButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_moreButton addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_moreButton];
        [self setTableMoreViewState:PETableMoreViewStateNormal];
    }
    return self;
}

#pragma mark public
- (void)tableMoreViewDidFinishedLoading
{
    [self setTableMoreViewState:PETableMoreViewStateNormal];
}

#pragma mark private
- (void)moreButtonClicked
{
    [self setTableMoreViewState:PETableMoreViewStateLoading];
    if (_delegate && [_delegate respondsToSelector:@selector(tableMoreDataIsLoading:)])
    {
        [_delegate tableMoreDataIsLoading:self];
    }
}

- (void)setTableMoreViewState:(PETableMoreViewState)state
{
    if (PETableMoreViewStateNormal == state)
    {
        _isLoading = NO;
        [_moreButton stopWaiting];
        [_moreButton setTitle:PETableMoreButtonTitleNormal forState:UIControlStateNormal];
        _moreButton.userInteractionEnabled = YES;
    }
    else if (PETableMoreViewStateLoading == state)
    {
        _isLoading = YES;
        CGFloat y = (_moreButton.bounds.size.height - 20)/2;
        [_moreButton startWaiting:CGRectMake(85, y, 20, 20)];
        [_moreButton setTitle:PETableMoreButtonTitleLoading forState:UIControlStateNormal];
        _moreButton.userInteractionEnabled = NO;
    }
}

@end

